﻿
package system.base;

import java.util.List;

import system.data.WarehousePageData;
import client.XMLSend;

import com.hitrust.gs.dao.GSOrderCategoryDAO;

import framework.core.Conditions;
import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DateUtil;
import framework.core.IHandler;
import framework.core.ILoginContext;
import framework.core.Logger;
import framework.core.ObjectList;
import framework.core.PageData;
import framework.core.SearchEvent;
import framework.core.SrvContext;
import framework.core.Value;
/*
	Author    :justin
	CreateTime:2007-12-13 17:36:20
*/
public class WarehouseSrv extends IHandler
{

	public WarehouseSrv() 
	{
		super();
	}
	private void deleteWarehouse(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		Warehouse obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = Warehouse.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveWarehouse(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		Warehouse obj;
		
		WarehousePageData data = (WarehousePageData)PageData.fromForm(context,WarehousePageData.class);
		//判断code是否已存在
		Warehouse ware=Warehouse.fetchByCode(con, data.getCode());
		if(ware!=null){
		    context.setErrorMsg("该编码已被使用！");
		    return;
		}
		if((id==null)||(id.equals("")))   //add
		{
			obj = Warehouse.fetch();
			data.setCreateUserId(loginContext.getLoginUserId());
			data.setCreateUser(loginContext.getLoginUserName());
    	}
    	else                     //update
    	{
    		obj = Warehouse.fetch(con,id);
    	}
		data.setLastUpdateUserId(loginContext.getLoginUserId());
		data.setLastUpdateUser(loginContext.getLoginUserName());
		int orderValue = Value.parseInt(data.getDisplayOrder(),-1);
		if (orderValue <= 0)
		{
		    data.setDisplayOrder("99");
		}
		
		context.setValue("WarehousePageData",data);
		obj.fill(data);
		obj.save(con);
		//荣爱接口通讯
		// 设置xml参数
        if (XMLSend.getInstance().isToRong()) {
            String actionName = "SubmitWareHouse";
            String dataName = "WareHouse";
            // 传输xml to 荣爱
            XMLSend.getInstance().submitWareHouseSendXML(actionName, dataName, data);

        }
	}
	private void listWarehouse(DBConnection con,SrvContext context,ILoginContext loginContext,String path) throws Exception
	{
		WarehouseSearchEvent event = (WarehouseSearchEvent)SearchEvent.fromForm(context,WarehouseSearchEvent.class);
		event.setWarehouseCodeUser(ConstDefine.getWildCard(loginContext.getLoginWarehouseCode()));
		context.setSessionValue("WarehouseSearchEvent",event);
		context.setNextUrl(path+"WarehouseList.jsp");
	}
	private void backWarehouse(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"WarehouseList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listWarehouse"))
		{
			listWarehouse(con,context,loginContext,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewWarehouse"))
		{
			String id = context.getString("Id",null);
			Warehouse obj = Warehouse.fetch(con,id);
			context.setValue("Warehouse",obj);
			context.setNextUrl(path+"WarehouseView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteWarehouse"))
		{
			context.setErrorUrl(path+"WarehouseList.jsp");
			deleteWarehouse(con,context);
			backWarehouse(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddWarehouse"))
		{
			Warehouse obj = Warehouse.fetch();
			WarehousePageData data = (WarehousePageData)PageData.fromTable(obj,WarehousePageData.class);
			context.setValue("WarehousePageData",data);
			context.setNextUrl(path+"WarehouseEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateWarehouse"))
		{
			String id = context.getString("Id",null);
			Warehouse obj = Warehouse.fetch(con,id);
			WarehousePageData data = (WarehousePageData)PageData.fromTable(obj,WarehousePageData.class);
			context.setValue("WarehousePageData",data);
			context.setNextUrl(path+"WarehouseEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyWarehouse"))
		{
			String id = context.getString("Id",null);
			Warehouse obj = Warehouse.copy(Warehouse.fetch(con,id));
			WarehousePageData data = (WarehousePageData)PageData.fromTable(obj,WarehousePageData.class);
			context.setValue("WarehousePageData",data);
			context.setNextUrl(path+"WarehouseEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveWarehouse"))
		{
			context.setErrorUrl(path+"WarehouseEdit.jsp");
			saveWarehouse(con,context,loginContext);
			backWarehouse(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewWarehouse"))
		{
			context.setErrorUrl(path+"WarehouseEdit.jsp");
			saveWarehouse(con,context,loginContext);
			Warehouse obj = Warehouse.fetch();
			WarehousePageData data = (WarehousePageData)PageData.fromTable(obj,WarehousePageData.class);
			context.setValue("WarehousePageData",data);
			context.setNextUrl(path+"WarehouseEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListWarehouse"))
		{
			backWarehouse(con,context,path);
			return;
		}
		
		//add by justin start

		//add by justin end
		
		//add by wing start
		
		//到仓库配送范围设置页面
		if(action_code.equalsIgnoreCase("toSendCustomer"))
        {
            String id = context.getString("Id",null);
            Warehouse warehouse = Warehouse.fetch(con,id);
            context.setSessionValue("WarehouseDetail",warehouse);
            WarehouseCustomerSearchEvent event = new WarehouseCustomerSearchEvent();
            event.setWarehouseCode(warehouse.getCode());
            event.setStatus(ConstDefine.STR_VALID_YES);
            ObjectList warehCustomerList = WarehouseCustomer.fetch().search(con, event);
            context.setSessionValue("WarehouseCustomerList", warehCustomerList);
            context.setNextUrl(path+"CustomerSend.jsp");
            return;
        }
        
		//保存仓库配送范围
        if(action_code.equalsIgnoreCase("saveCustomerSend"))
        {
            context.setErrorUrl(path+"CustomerSend.jsp");
            saveCustomerSend(con,context,loginContext);
            backWarehouse(con,context,path);
            return;
        }
        //add by wing end
		//到仓库申购分类设置页面
		if(action_code.equalsIgnoreCase("toOrderCategory"))
        {
            String id = context.getString("Id",null);
            Warehouse warehouse = Warehouse.fetch(con,id);
            context.setSessionValue("WarehouseDetail",warehouse);
            String productType = context.getString("productType",null);
            if(productType!=null){
            	GSOrderCategoryDAO dao= new GSOrderCategoryDAO();
            	List all =dao.queryProviderByBid(productType);
            	List yes=dao.queryProviderByOrderCategory(productType, warehouse.getCode());
            	context.setSessionValue("all", all);//所有分类下的供应商
            	context.setSessionValue("yes", yes);//仓库对应分类的供应商
            }else{
            	context.setSessionValue("all", null);
            	context.setSessionValue("yes", null);            	
            }   
            context.setSessionValue("productType", productType);
            context.setNextUrl(path+"OrderCategory.jsp");
            return;
        }
		//保存仓库申购分类设置页面
        if(action_code.equalsIgnoreCase("saveOrderCategory"))
        {
            context.setErrorUrl(path+"OrderCategory.jsp");
            saveOrderCategory(con,context,loginContext);
            backWarehouse(con,context,path);
            return;
        }
      //到中心库品牌设置页面
		if(action_code.equalsIgnoreCase("toCenterOrderCategory"))
        {
            String id = context.getString("Id",null);
            Warehouse warehouse = Warehouse.fetch(con,id);
            context.setSessionValue("WarehouseDetail",warehouse);
            String productType = context.getString("productType",null);
            if(warehouse!=null){
            	GSOrderCategoryDAO dao= new GSOrderCategoryDAO();
            	List all =dao.queryProductBrand();
            	List yes=dao.queryProductBrandCheck(warehouse.getCode());
            	context.setSessionValue("all", all);//所有分类下的供应商
            	context.setSessionValue("yes", yes);//仓库对应分类的供应商
            }else{
            	context.setSessionValue("all", null);
            	context.setSessionValue("yes", null);            	
            }   
            context.setSessionValue("productType", productType);
            context.setNextUrl(path+"CenterProductBrand.jsp");
            return;
        }
		//保存仓库申购分类设置页面
        if(action_code.equalsIgnoreCase("saveCenterProductBrand"))
        {
            context.setErrorUrl(path+"CenterProductBrand.jsp");
            saveCenterProductBrand(con,context,loginContext);
            backWarehouse(con,context,path);
            return;
        }
		Logger.throwException("WarehouseSrv","unknown action_code="+action_code);
	}
	
	
    //保存   选中的 中心库品牌
    private void saveCenterProductBrand(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
    {
        String warehouseCode = context.getString("warehouseCode",null);
        String []customerCode = context.getParameterValues("customer"); 
        GSOrderCategoryDAO dao= new GSOrderCategoryDAO();
        if (warehouseCode!= null)
        {   
        	dao.delete2(warehouseCode);
        	if(customerCode!=null){
           	 for(int i=0;i<customerCode.length;i++){
     		 	dao.add2(warehouseCode, customerCode[i]);                                    
             }
        	}
        	
            

            
        }
    }
	
	//add by wing start
    
	/*
    //保存仓库配送范围
    private void saveCustomerSend(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
    {
        String warehouseCode = context.getString("warehouseCode",null);
        WarehouseCustomerSearchEvent warehCustomerevent = new WarehouseCustomerSearchEvent();
        warehCustomerevent.setWarehouseCode(warehouseCode);
        WarehouseCustomer warehouseCustomer = WarehouseCustomer.fetch(con, warehCustomerevent.toConditions());
        if(warehouseCustomer!=null) 
            warehouseCustomer.deleteByWarehouseCode(con, warehouseCode);
        
        String []customerCode = context.getParameterValues("CustomerCode");
        for(int i=0;i<customerCode.length;i++){
            WarehouseCustomer obj = WarehouseCustomer.fetch();
            WarehouseCustomerPageData data = (WarehouseCustomerPageData)PageData.fromForm(context,WarehouseCustomerPageData.class);
            data.setWarehouseCode(warehouseCode);
            data.setCreateUserId(loginContext.getLoginUserId());
            data.setCreateUser(loginContext.getLoginUserName());
            
//            Customer cobj = Customer.fetch(con,customerCode[i]);
//            if (cobj!=null){
                data.setCustomerCode(customerCode[i]);
//            }
            data.setLastUpdateUserId(loginContext.getLoginUserId());
            data.setLastUpdateUser(loginContext.getLoginUserName());
            obj.fill(data);
            obj.save(con);
        }
    }
    */
    //保存仓库申购分类设置页面
    private void saveOrderCategory(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
    {
        String warehouseCode = context.getString("warehouseCode",null);
        String []customerCode = context.getParameterValues("CustomerCode"); 
        String productType = context.getString("productType",null);
        GSOrderCategoryDAO dao= new GSOrderCategoryDAO();
        if (warehouseCode!= null&&productType !=null)
        {   
        	dao.delete(productType, warehouseCode);
        	if(customerCode!=null){
           	 for(int i=0;i<customerCode.length;i++){
     		 	dao.add(warehouseCode, customerCode[i], productType);                                    
             }
        	}
        	
            

            
        }
    }
    //保存仓库配送范围
    private void saveCustomerSend(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
    {
        String warehouseCode = context.getString("warehouseCode",null);
        String []customerCode = context.getParameterValues("CustomerCode");        
        
        Conditions cs =new Conditions();
        cs.add(WarehouseCustomer.WAREHOUSE_CODE,warehouseCode);
        ObjectList list = WarehouseCustomer.fetch().search(con, cs);
        
        if (customerCode == null)
        {
            for(int i=0;i<list.size();i++)
            {
                WarehouseCustomer wc = (WarehouseCustomer)list.get(i);
                wc.setLastUpdateTime(new DateUtil().getFullString());
                wc.setLastUpdateUserId(loginContext.getLoginUserId());
                wc.setLastUpdateUser(loginContext.getLoginUserName());
                wc.setStatus(ConstDefine.STR_VALID_NO);
                wc.save(con);
            }
        }
        else
        {
            
            for(int i=0;i<list.size();i++)
            {
                WarehouseCustomer wc = (WarehouseCustomer)list.get(i);
                if (isExistWarehouseCustomer(customerCode,wc.getCustomerCode()))
                {
                    wc.setLastUpdateTime(new DateUtil().getFullString());
                    wc.setLastUpdateUserId(loginContext.getLoginUserId());
                    wc.setLastUpdateUser(loginContext.getLoginUserName());
                    wc.setStatus(ConstDefine.STR_VALID_YES);
                    wc.save(con);
                }
                else
                {
                    wc.setLastUpdateTime(new DateUtil().getFullString());
                    wc.setLastUpdateUserId(loginContext.getLoginUserId());
                    wc.setLastUpdateUser(loginContext.getLoginUserName());
                    wc.setStatus(ConstDefine.STR_VALID_NO);
                    wc.save(con);
                }
            }  
            
            for(int i=0;i<customerCode.length;i++){
                if (isNotExistWarehouseCustomer(list,customerCode[i]))
                {
                    WarehouseCustomer obj = WarehouseCustomer.fetch();
                    obj.setWarehouseCode(warehouseCode);
                    obj.setCreateUserId(loginContext.getLoginUserId());
                    obj.setCreateUser(loginContext.getLoginUserName());
                    obj.setCreateTime(new DateUtil().getFullString());
                    obj.setCustomerCode(customerCode[i]);
                    obj.setLastUpdateUserId(loginContext.getLoginUserId());
                    obj.setLastUpdateUser(loginContext.getLoginUserName());
                    obj.setCreateTime(new DateUtil().getFullString());
                    obj.setStatus(ConstDefine.STR_VALID_YES);
                    obj.save(con);                   
                }
            }
        }
    }
    
    private boolean isExistWarehouseCustomer(String []customerCode,String warehouseCustomerCode){
        for (int tI = 0; tI < customerCode.length; tI++) {
            if (customerCode[tI].equals(warehouseCustomerCode)) return true;
        }
        return false;
    }
    
    private boolean isNotExistWarehouseCustomer(ObjectList list,String customerCode){
        try{
            for(int i=0;i<list.size();i++)
            {
                WarehouseCustomer wc = (WarehouseCustomer)list.get(i);
                if (wc.getCustomerCode().equals(customerCode))
                {
                    return false;
                }                    
            }
        }
        catch(Exception e)
        {
            return false;
        }
        return true;
    }
    
    //add by wing end
}
